package output;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;
import organization.Base;
import person.PersonInfo;
import person.Teacher;

public class TeacherConsole extends Console {

private Teacher tchr= new Teacher();
private SimpleDateFormat format3 = new SimpleDateFormat("dd.MM.yyyy");

	public TeacherConsole (){
		if (consoleStr.size() == 0) {
			consoleStr.add("Get all Teachers List;");
			consoleStr.add("Add new Teacher;");
			consoleStr.add("Get all info by name;");
			consoleStr.add("Set Study Subject;");
			consoleStr.add("Return to main menu.");
		}
	}


	public void inputInConsole (){
		Scanner scan = new Scanner(System.in);
		int opt;
		while (true) {
			while (scan.hasNextLine()) {
				if (scan.hasNextInt()) {
					opt = scan.nextInt();
					if (1 <= opt & opt <= consoleStr.size()) {
						if (opt == 1) {
							getAllTeachers();
							getConsole();
							return;
						}
						if (opt == 2) {
							addTeacher (AddPersonal());
							getConsole();
							return;
						}
						if (opt == 3) {
							getTeachByName ();
							getConsole();
							return;
						}
						if (opt == 4) {
							setSuject ();
							getConsole();
							return;
						}
						if (opt == 5){
							return;
						}
					}
					System.out.println("Please enter option:");
				}else {
					System.out.println("Input incorrect!Please enter option:");
					scan.next();
				}
			}
		}
	}
	private void getAllTeachers (){
		ArrayList <Teacher> Teachers = Base.getSortlist(tchr);
		if (Teachers.size() == 0) {
			System.out.println("Teachers list is empty!");
		} else {
			for (int i = 0; i < Teachers.size(); i++) {
				System.out.println("Name: "
						+ Teachers.get(i).getFirstAndScndName() + ""
						+ ".\r\nBorn: " + Teachers.get(i).getPlaceOfBirth());
				System.out.print("Study:");
				ArrayList<String> Subjects = Teachers.get(i)
						.getStudySubjectList();
				if (Subjects.size() == 0) {
					System.out.println("No Subjects set.");
				} else {
					for (int j = 0; j < Subjects.size(); j++) {
						System.out.println((j + 1) + ". " + Subjects.get(j)+ ";");
					}
				}
			}
		}
	}
	private void addTeacher (PersonInfo info){
		if (info != null){
			Base.addPerson(new Teacher(info));
		}
	}
	
	Teacher getTeachByName (){
		System.out.println ("Pelase enter teacher first name:");
		Scanner scan = new Scanner (System.in);
		String firstName = scan.next();
		System.out.println("Please enter teacher second name:");
		String secndName = scan.next();
		
		
		Teacher parsedTeacher = Base.getPersonByName(firstName, secndName, tchr);
		if (parsedTeacher != null) {
			System.out.println("Name:" + parsedTeacher.getFirstAndScndName()
					+ ";");
			System.out.println("Born in:" + parsedTeacher.getPlaceOfBirth()
					+ " " + format3.format(parsedTeacher.getBirth().getTime())
					+ ";");
			
		}
		return parsedTeacher;
	
	}
	private void setSuject (){
		Teacher teacher = getTeachByName();
		Scanner scan = new Scanner (System.in);
		System.out.println("Please enter Subject:");
		teacher.addStudySubject(scan.next());
		System.out.println("Subject added.");
	}
}
